Ext.define('TS.view.rp.RpAPayBillEditor',{
	extend: 'Ext.window.Window',
	alias: 'widget.rpapaybilleditor',
	title: '应付单',
	width: 900,
	border: false,
	bodyPadding: 0,
	modal:true,
    layout: {
        type: 'vbox',
        align: 'stretch'
    },	
    fieldDefaults: {
        labelAlign: 'left',
        labelWidth: 60,
        labelStyle: 'font-weight:bold'
    },
    defaults: {
        anchor: '100%',
        margins: '0 0 10 0'
    },
    buttons:[{
    	action: 'save',
    	iconCls:'button-ok',
    	text: '保存',
    	iconCls: 'button-save'
    },{
    	text: '关闭',
    	iconCls: 'button-close',
    	handler:function() {
    		var win = this.up('window');
    		win.close();
    	}
    }],
	initComponent:function() {
		//初始化业务员列表
		var store = Ext.create('TS.store.base.EmpStore');
		var invComp = Ext.ComponentQuery.query('accmenu combobox')[0];
		var invModel = invComp.findRecordByValue(invComp.getValue());
		var deptId = invModel.get('deptId');
		store.load({params:{deptId:deptId}});
		
		//创建应收金额验证器
		var unzerovalid = Ext.apply(Ext.form.field.VTypes,{
			unzero:function(value,field) {
				return value != 0;
			},
			unzeroText:'金额不能为零!'
		});
		this.items = [
		    {
		    	xtype:'form',
		    	items:[{
		        	html : '<h1 align=center><font color=red>应&ensp;付&ensp;单</font></h1>'
		        },{
		        	xtype: 'fieldcontainer',
		        	labelStyle: 'font-weight:bold;padding:0',
		        	layout: 'hbox',
		        	defaultType: 'textfield',    	
		            items:[{
		    	    	xtype: 'triggerfield',
		    	    	emptyText:'请输入客户编码或名称',
		    	    	trigger1Cls:Ext.baseCSSPrefix + 'form-search-trigger',
		    	    	onTriggerClick:function(e){e.keyCode=e.ENTER;this.fireEvent('specialkey',this,e);},
		            	name: 'customerCode',
		            	flex:1,
		            	fieldLabel: '客户编码',
		            	labelStyle:'text-align:right',
		            	allowBlank:false,
		            	listeners: {
		            		specialkey:function(field,e) {
		            			var custIdField = field.up('window').down('hiddenfield[name=customerId]');
		            			if(e.getKey() == e.ENTER) {
		            			    	var view=Ext.create('TS.view.base.CustomerSearchDialog');
		            			    	view.setVal(field.value);
		            			    	view.on({
		            					    selected: function(records){
		            					    	if(records.length == 0) {
		            					    		Ext.MessageBox.alert('ThinkGsp','未检索到任何客户信息!');
		            					    	} else {
		            						    	field.setValue(records[0].get('customerCode'));
		            						    	var nameField = field.up('window').down('textfield[name=customerName]');
		            						    	nameField.setValue(records[0].get('customerName'));
		            						    	var form = field.up('form');
		            						    	form.getRecord().setcustomer(records[0]);
		            						    	form.getRecord().set('custSuppId',records[0].get('customerId'));
		            					    	}
		            					    },
		            					    scope: this
		            					});
		            			    	view.show();	
		            		    }        			
		            		}
		            	}
		            },{
		            	xtype: 'textfield',
		            	readOnly:true,
		            	flex:1,
		            	name: 'customerName',
		            	labelStyle:'text-align:right',
		            	fieldLabel: '客户名称'
		            }]
		        },{
		        	xtype: 'fieldcontainer',
		        	layout: 'hbox',
		        	items:[{
		        		flex: 1,
		        		xtype:'textfield',
		        		name:'arpBillName',
		        		fieldLabel: '单据类型',
		        		labelStyle:'text-align:right',
		        		readOnly:true,
		        		allowBlank:false
		        	},{
		        		flex: 1,
		        		xtype:'combobox',
		        		labelStyle:'text-align:right',
		        		fieldLabel: '部门',
		        		name:'deptName',
		        		allowBlank:false
		        	}]
		        },{
		        	xtype: 'fieldcontainer',
		        	layout: 'hbox',
		        	items:[{
		            	xtype: 'textfield',
		            	readOnly:true,
		            	flex:1,
		            	name:'arpBillCode',
		            	labelStyle: 'text-align:right',
		            	fieldLabel: '单据编码',
		            	allowBlank:false
		        	},{
		        		flex:1,
		        		xtype:'combobox',
		        		labelStyle:'text-align:right',
		        		fieldLabel: '业务员',
		        		name:'empId',
						queryMode:'local',
						valueField:"empId",
						displayField :'empName',
						forceSelection:true,
						typeAhead : true,
						allowBlank: false,
						store:store,
						listeners:{
							select:function(combo,records) {
								var model = this.up('form').getRecord();
								model.setemp(records[0]);
							}
						}
		        	}]
		        },{
		        	xtype: 'fieldcontainer',
		        	layout:'hbox',
		        	items:[{
		            	flex:1,
		        		xtype:'datefield',
		        		name:'billDate',
		        		labelStyle:'text-align:right',
		        		fieldLabel: '单据日期',
		        		format:'Y-m-d',
		        		allowBlank:false
		            },{
		        		flex:1,
		        		xtype:'textfield',
		        		readOnly:true,
		        		labelStyle:'text-align:right',
		        		fieldLabel:'制单人',
		        		name:'makerName',
		        		allowBlank:false
		        	}]
		        },{
		        	xtype: 'fieldcontainer',
		        	layout: 'hbox',
		        	items:[{
		        		flex:1,
		        		xtype: 'numberfield',
		        		name:'amount',
		        		labelStyle:'text-align:right',
		        		fieldLabel:'应收金额',
		        		allowBlank: false,
		        		fieldStyle:'text-align:right',
		        		allowDecimals: true,
		        		vtype:'unzero'
		        	},{
		        		flex:1,
		        		xtype: 'datefield',
		        		name:'arpDate',
		        		labelStyle:'text-align:right',
		        		fieldLabel: '应收日期',
		        		format:'Y-m-d'
		        	}]
		        },{
		        	xtype: 'textareafield',
		        	name:'remark',
		        	labelStyle:'text-align:right',
		        	height: '80',
		        	anchor: '100%',
		        	fieldLabel: '备注'
		        }]
		    }
		];
		this.callParent(arguments);
	},
	listeners:{
		show:{
			fn:function(combobox) {
				var form = this.down('form');
				var model = form.getRecord();
	        	var deptField = form.down('combobox[name=deptName]');
	        	deptField.setValue(model.getdept().get('deptName'));
	        	var makerField = form.down('textfield[name=makerName]');
	        	makerField.setValue(model.getmaker().get('empName'));
			}
		}
	},
	//初始化Combobox
    initAssociation: function(model) {
    	var deptField = this.down('form').down('combobox[name=deptName]');
    	deptField.setValue(model.getdept().get('deptName'));
    	var empField = this.down('form').down('combobox[name=empId]');
    	empField.select(model.getemp().get('empId'));
    	var makerField = this.down('form').down('textfield[name=makerName]');
    	makerField.setValue(model.getmaker().get('empName'));
    	if(model.customerHasOneInstance != null) {
    		this.down('form').down('triggerfield').setValue(model.getcustomer().get('customerCode'));
    		this.down('form').down('textfield[name=customerName]').setValue(model.getcustomer().get('customerName'));
    	}
    	if(model.get('archived')) {
    		this.down('button[name=save]').disabled = true;
    	}
    }        
});